package javaWeb;

import Bean.Furn;
import Bean.Page;
import Service.FurnService;
import Service.Impl.FurnServiceImpl;
import Service.Impl.PageServiceImpl;
import Service.PageService;
import Utils.DBHelp;
import Utils.Tool;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;

public class FurnServlet extends BasicServlet {
    private FurnService furnService = new FurnServiceImpl();
    private int ManagePageSize = 4;
    public void List(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //这里是分配page 该怎么解决的
        Object isFirst = req.getAttribute("isFirst");
        Page page = null;
        HttpSession session = req.getSession();
        if (isFirst == null){
            page = furnService.initManagePage(new Page(), ManagePageSize);
        }else {
            page = (Page) session.getAttribute("Page");
        }
        List<Furn> allFurn = furnService.getSomeFurn(page);
        session.setAttribute("Furns",allFurn);
        session.setAttribute("FurnPage",page);
        System.out.println("daozhe");
        resp.sendRedirect("/Web/Furn/furn_manage.jsp");

    }
    public void addFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        // 获得
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String name = req.getParameter("name");
        String maker = req.getParameter("maker");
        String price = req.getParameter("price");
        String sales = req.getParameter("sales");
        String stock = req.getParameter("stock");
        String img = req.getParameter("img");
        String index = "no";
        if(!Tool.isNULL(name,maker,price,price,sales,stock,img)) {
            req.setAttribute("meg", "数据不能为空");
            req.getRequestDispatcher("/Furn/furn_add.jsp").forward(req, resp);
            return;
        }
        if (!Tool.PositiveNumber(price,sales,stock)){
            req.setAttribute("meg", "数量要求为正数");
            req.getRequestDispatcher("/Furn/furn_add.jsp").forward(req, resp);
            return;
        }
        Furn furn = new Furn();
        try {
            BeanUtils.populate(furn,req.getParameterMap());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } finally {
        }
        furn = Tool.copy(req.getParameterMap(),furn);
        if (furnService.insertFurn(furn) ){
            req.setAttribute("meg","成功");
            resp.sendRedirect(req.getContextPath()+"/javaWeb/FurnServlet?action=List");
        }else {
            req.setAttribute("meg","添加数据格式错误");
            req.getRequestDispatcher("/Furn/furn_add.jsp").forward(req,resp);
            return;
        }
    }
    protected void deleteFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String furnName = req.getParameter("FurnName");
        String merchant = req.getParameter("Merchant");
        HttpSession session = req.getSession();
        if (furnService.deleteFurn(furnName,merchant)){
            session.setAttribute("Furns",furnService.getAllFurn());
            req.setAttribute("meg","");
        }else {
            req.setAttribute("meg","删除失败");
        }
        resp.sendRedirect("/Web/Furn/furn_manage.jsp");
    }
    protected void getFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String id = req.getParameter("id");
        String sql = "select id, FurnName, Merchant, Price, Volume, inventory, img_path imgPath from Furn where id = ?";
        Furn furn = furnService.getFurn(sql, id);
        HttpSession session = req.getSession();
        session.setAttribute("furn",furn);
        resp.sendRedirect("/Web/Furn/furn_update.jsp");
       // req.getRequestDispatcher("").forward(req,resp);
    }
    protected void updateFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String FurnName = req.getParameter("name");
        String Merchant = req.getParameter("maker");
        String price = req.getParameter("price");
        String Volume = req.getParameter("sales");
        String inventory = req.getParameter("stock");
        String imgPath = req.getParameter("img");
        String id = req.getParameter("id");


        if(!Tool.isNULL(FurnName,Merchant,price,price,Volume,inventory,imgPath)) {
            System.out.println(1);
            req.setAttribute("meg", "数据不能为空");
            req.getRequestDispatcher("/furn/furn_update.jsp").forward(req,resp);
            return;
        }
        if (!Tool.PositiveNumber(Volume,inventory)){
            req.setAttribute("meg", "添加的应该为正数");
            req.getRequestDispatcher("/furn/furn_update.jsp").forward(req,resp);
            return;
        }
        if (!Tool.Double(price)){
            req.setAttribute("meg", "格式错误");
            req.getRequestDispatcher("/Furn/furn_update.jsp").forward(req,resp);
            return;
        }
        String sql = "select id, FurnName, Merchant, Price, Volume, inventory, img_path imgPath from Furn where id = ?;";
       Furn before = furnService.getFurn(sql,id);
        Furn current = new Furn();


        /*
         private Integer id;
    private String FurnName;
    // 商家
    private String Merchant;
    //BigDecimal 指定特殊精度，比Double好
    // 也防止空指针
    private BigDecimal Price;
    //销量
    private Integer Volume;
    // 库存
    private Integer inventory;
    //隐藏坑 字段名与数据库名不一致，要特殊解决
    private String imgPath = "web/assets/images/product-image/default.jpg";
         */
        try {
            current.setId(Integer.parseInt(id));
            current.setFurnName(FurnName);
            current.setMerchant(Merchant);
            current.setPrice(new BigDecimal(price));
            current.setVolume(Integer.parseInt(Volume));
            current.setInventory(Integer.parseInt(inventory));
            current.setImgPath(imgPath);
        } catch (NumberFormatException e) {
            req.setAttribute("meg", "格式错误");
            req.getRequestDispatcher("/Furn/furn_update.jsp").forward(req,resp);
            return;
        } finally {
        }
        System.out.println(before);
        System.out.println(current);
        if (furnService.updateFurn(current.getId(),current)){
        }
        List(req, resp);
    }

    protected void Page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //这里是分配page 该怎么解决的
//        String pageNo = req.getParameter("PageNo");
//        Page<Furn> pageFurn = furnService.getPage_Furn(Tool.getNumber(pageNo, 1), ManagePageSize);
//        HttpSession session = req.getSession();
//        session.setAttribute("FurnPage",pageFurn);
        resp.sendRedirect("/Web/javaWeb/CustomerFurnPage?action=FurnPage");
    }

}
